package com.ibm.cps.servlet;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ibm.cps.processors.ProcessorFactory;
import com.ibm.cps.processors.ProcessorsGroup;
import com.ibm.util.ErrorCode;
import com.ibm.util.JSONUtility;
import com.ibm.util.exception.CPSException;

public class AssignIdFactory {

	public static String getProcessorid(String type, ObjectNode object)
			throws CPSException {
		String processorid;
		String tag;
		if (ProcessorsGroup.isGroupType(type)) {
			tag = ProcessorsGroup.getGroupTypeFieldName(type);
		} else if (ProcessorFactory.getInstance().isProcessorType(type)) {
			tag = ProcessorFactory.getIdFieldName(type);
		} else {
			throw new CPSException(ErrorCode.ERROR_PROCESSOR_TYPE_CODE,
					"Unknown type " + type);
		}
		if (object.get(tag) == null) {
			processorid = JSONUtility.assignRandomValue(object, tag);
		} else {
			processorid = JSONUtility.GetString(tag, object);
		}
		return processorid;
	}

}
